package cn.schoolwow.util.test.module.query.resource.testcase;

import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.util.domain.query.resource.QueryResource;
import cn.schoolwow.util.domain.query.resource.QueryResourceResponse;
import cn.schoolwow.util.module.query.resource.domain.SingleQueryResourceHandler;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.junit.Assert;

import java.net.URL;
import java.util.concurrent.atomic.AtomicInteger;

public class WalkResourceTestCaseFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        AtomicInteger atomicInteger = new AtomicInteger(0);

        QueryResource.newQuery("用户角色组合列表", "module/query/resource/walk")
                .fileNameEndWith(".json")
                .singleQueryResourceHandler(new SingleQueryResourceHandler() {
                    @Override
                    public boolean handleQueryResourceResponse(QueryResourceResponse queryResourceResponse) {
                        URL url = queryResourceResponse.getAsURL();

                        switch (url.getFile()){
                            case "role.json":{
                                JSONArray array = queryResourceResponse.getAsJSONArray();
                                Assert.assertEquals(1, array.size());

                                JSONObject o = array.getJSONObject(0);
                                Assert.assertEquals("admin", o.getString("roleKey"));
                                Assert.assertEquals("管理员", o.getString("roleName"));
                            }break;
                            case "user1.json":{
                                JSONObject user = queryResourceResponse.getAsJSONObject();
                                Assert.assertEquals("zhangsan", user.getString("username"));
                                Assert.assertEquals("123456", user.getString("password"));
                            }break;
                            case "user2.json":{
                                JSONObject user = queryResourceResponse.getAsJSONObject();
                                Assert.assertEquals("lisi", user.getString("username"));
                                Assert.assertEquals("123456", user.getString("password"));
                            }break;
                        }
                        atomicInteger.set(1);
                        return true;
                    }
                })
                .execute()
                .walkResource();
        if(atomicInteger.get()==0){
            throw new IllegalArgumentException("walkResource遍历资源为空");
        }
    }

    @Override
    public String name() {
        return "遍历文件夹测试用例";
    }
}
